/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.propertytable;

import java.awt.Color;
import java.util.HashMap;

import javax.swing.JTable;
import com.acarter.propertytable.celleditor.BooleanEditor;

import javax.swing.event.MouseInputAdapter;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import com.acarter.propertytable.celleditor.DoubleEditor;
import com.acarter.propertytable.celleditor.EnumEditor;
import com.acarter.propertytable.celleditor.FloatEditor;
import com.acarter.propertytable.celleditor.IntegerEditor;
import com.acarter.propertytable.celleditor.PropertySectionEditor;
import com.acarter.propertytable.celleditor.PropertySectionStateEditor;
import com.acarter.propertytable.celleditor.StringEditor;
import com.acarter.propertytable.cellrenderer.EnumRenderer;
import com.acarter.propertytable.cellrenderer.FloatRenderer;
import com.acarter.propertytable.cellrenderer.DoubleRenderer;
import com.acarter.propertytable.cellrenderer.IntegerRenderer;
import com.acarter.propertytable.cellrenderer.PropertyBorderRenderer;
import com.acarter.propertytable.cellrenderer.BooleanRenderer;
import com.acarter.propertytable.cellrenderer.PropertySectionRenderer;
import com.acarter.propertytable.cellrenderer.PropertySectionStateRenderer;
import com.acarter.propertytable.cellrenderer.StringRenderer;
import com.acarter.propertytable.propertyobject.BooleanPropertyObject;
import com.acarter.propertytable.propertyobject.DoublePropertyObject;
import com.acarter.propertytable.propertyobject.EnumPropertyObject;
import com.acarter.propertytable.propertyobject.FloatPropertyObject;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject;
import com.acarter.propertytable.propertyobject.StringPropertyObject;

/**
 * @author Carter
 * 
 */
public class PropertyTable extends JTable {

	/**	 */
	private static final long serialVersionUID = 1L;

	/**  */
	protected MouseInputAdapter rowResizer = null;

	/**  */
	protected MouseInputAdapter columnResizer = null;

	/**  */
	private HashMap<Class<?>, TableCellEditor> editors = new HashMap<Class<?>, TableCellEditor>();

	/**  */
	private HashMap<Class<?>, TableCellRenderer> renderers = new HashMap<Class<?>, TableCellRenderer>();

	/**	 */
	public static Color borderColor = new Color(214, 225, 249);

	/**
	 * @param model
	 */
	public PropertyTable(TableModel model) {
		super(model);

		renderers.put(null, new PropertyBorderRenderer());
		
		renderers.put(PropertySectionState.class, new PropertySectionStateRenderer());
		renderers.put(PropertySection.class, new PropertySectionRenderer());
		renderers.put(BooleanPropertyObject.class, new BooleanRenderer());
		renderers.put(IntegerPropertyObject.class, new IntegerRenderer());
		renderers.put(FloatPropertyObject.class, new FloatRenderer());
		renderers.put(DoublePropertyObject.class, new DoubleRenderer());
		renderers.put(EnumPropertyObject.class, new EnumRenderer());
		renderers.put(StringPropertyObject.class, new StringRenderer());

		editors.put(PropertySectionState.class, new PropertySectionStateEditor());
		editors.put(PropertySection.class, new PropertySectionEditor());
		editors.put(BooleanPropertyObject.class, new BooleanEditor());
		editors.put(IntegerPropertyObject.class, new IntegerEditor());
		editors.put(FloatPropertyObject.class, new FloatEditor());
		editors.put(DoublePropertyObject.class, new DoubleEditor());
		editors.put(EnumPropertyObject.class, new EnumEditor());
		editors.put(StringPropertyObject.class, new StringEditor());

		setColumnConstraints();

		setRowHeight(18);
		setGridColor(borderColor);
		setShowVerticalLines(false);
	}
	
	public void setColumnConstraints() {
		
		TableColumn c = getColumnModel().getColumn(0);
		c.setPreferredWidth(10);
		c.setMaxWidth(c.getPreferredWidth());
		c.setResizable(false);
		
		c = getColumnModel().getColumn(2);
		c.setResizable(false);
		
		getTableHeader().setReorderingAllowed(false);
	}

	/**
	 * 
	 * @param c
	 * @param renderer
	 */
	public void addCellRenderer(Class<?> c, TableCellRenderer renderer) {

		renderers.put(c, renderer);
	}

	/**
	 * 
	 * @param c
	 * @param editor
	 */
	public void addCellEditor(Class<?> c, TableCellEditor editor) {

		editors.put(c, editor);
	}

	/**
	 * 
	 * @param c
	 * @param renderer
	 */
	public TableCellRenderer getCellRenderer(Class<?> c) {

		return renderers.get(c);
	}

	/**
	 * 
	 */
	@Override
	public TableCellRenderer getCellRenderer(int row, int col) {

		TableCellRenderer renderer = null;

		Object object = getModel().getValueAt(row, col);

		if (object != null)
			renderer = renderers.get(object.getClass());
		else
			renderer = renderers.get(null);

		return ((renderer != null) ? renderer : super.getCellRenderer(row, col));
	}

	/**
	 * 
	 * @param c
	 * @param editor
	 */
	public TableCellEditor getCellEditor(Class<?> c) {

		return editors.get(c);
	}

	/**
	 * 
	 */
	@Override
	public TableCellEditor getCellEditor(int row, int col) {

		TableCellEditor editor = null;

		Object object = getModel().getValueAt(row, col);

		if (object != null)
			editor = editors.get(object.getClass());
		else

			editor = editors.get(null);

		return ((editor != null) ? editor : super.getCellEditor(row, col));
	}

	/**
	 * turn resizing on/of
	 */
	public void setResizable(boolean row, boolean column) {

		if (row) {

			if (rowResizer == null)
				rowResizer = new PropertyTableRowResizer(this);
		} 
		else if (rowResizer != null) {

			removeMouseListener(rowResizer);
			removeMouseMotionListener(rowResizer);
			rowResizer = null;
		}
		if (column) {
			
			if (columnResizer == null)
				columnResizer = new PropertyTableColumnResizer(this);
			
		} 
		else if (columnResizer != null) {
			
			removeMouseListener(columnResizer);
			removeMouseMotionListener(columnResizer);
			columnResizer = null;
		}
	}

	/**
	 * mouse press intended for resize shouldn't change row/col/cell celection
	 */
	public void changeSelection(int row, int column, boolean toggle, boolean extend) {

		if (getCursor() == PropertyTableColumnResizer.resizeCursor || getCursor() == PropertyTableRowResizer.resizeCursor)
			return;

		super.changeSelection(row, column, toggle, extend);
	}

}
